
package com.zhu.salesmanagement;

import java.util.ArrayList;
import java.util.List;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.StoreEvaluationActivity.MyOnPageChangeListener;
import com.zhu.salesmanagement.StoreEvaluationActivity.MyPagerAdapter;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.models.PusherStoreItem;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.views.GuideInfoView;
import com.zhu.salesmanagement.views.StoreDataView;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

/**
 * 导购信息界面
 */
public class StoreGuideInfoActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "zhu";
    private static final int PLIST_QUERY_TOKEN = 0;

    private String sCustomerID;
    private ViewPager mViewPager = null;
    private DataQueryHandler mDataQueryHandler = null;
    private List<PusherStoreItem> mPusherList = new ArrayList<PusherStoreItem>();
    private List<View> mPusherViewList = new ArrayList<View>();
    private TextView mNone = null;
    private TextView mClew = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        sCustomerID = this.getIntent().getStringExtra("cid");
        setContentView(R.layout.layout_storeguide_view);

        initView();
        initData();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        findViewById(R.id.storeguide_back).setOnClickListener(this);
        findViewById(R.id.storeguide_bottom_back).setOnClickListener(this);
        mNone = (TextView)findViewById(R.id.s_none);
        mViewPager = (ViewPager)findViewById(R.id.s_vPager);
        mClew = (TextView)findViewById(R.id.s_clew);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        String where = SalesSettings.PusherInfoColumns.STOREID + " = \'" + sCustomerID +"\'";
        mDataQueryHandler = new DataQueryHandler(this.getContentResolver());
        mDataQueryHandler.startQuery(PLIST_QUERY_TOKEN, null, SalesSettings.PusherInfoColumns.CONTENT_URI, null, where, null, null);
    }

    /**
     * 点击事件处理
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.storeguide_back:
            case R.id.storeguide_bottom_back:
                finish();
                break;
        }
    }

    /**
     * 更新导购数据
     * @param cursor 数据库数据
     */
    private void updateList(Cursor cursor){
        mPusherList.clear();
        try {
            final int nameIndex = cursor.getColumnIndexOrThrow(SalesSettings.PusherInfoColumns.ESNAME);
            final int escIndex = cursor.getColumnIndexOrThrow(SalesSettings.PusherInfoColumns.ESC);
            final int acdIndex = cursor.getColumnIndexOrThrow(SalesSettings.PusherInfoColumns.ACD);
            final int mobileIndex = cursor.getColumnIndexOrThrow(SalesSettings.PusherInfoColumns.MOBILE);
            final int dataIndex = cursor.getColumnIndexOrThrow(SalesSettings.PusherInfoColumns.RDATA);
            
            while (cursor.moveToNext()) {
                PusherStoreItem item = new PusherStoreItem();
                item.sEmployeeName = cursor.getString(nameIndex);
                item.sEmployeeCategory = cursor.getString(escIndex);
                item.sAccreditCategoryDescription = cursor.getString(acdIndex);
                item.sMobile = cursor.getString(mobileIndex);
                item.dRegisterDate = cursor.getLong(dataIndex);
                mPusherList.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateView error");
        } finally {
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }

        updateView();
    }

    /**
     * 更新界面
     */
    private void updateView(){
        int size = mPusherList.size();
        if(size > 0){
            mNone.setVisibility(View.GONE);
            for(int i = 0; i < size; i++){
                View v = getLayoutInflater().inflate(R.layout.storeguide_view, null);
                mPusherViewList.add(v);
            }
            mViewPager.setAdapter(new MyPagerAdapter(mPusherViewList));
            mViewPager.setCurrentItem(0);
            mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
            mViewPager.setVisibility(View.VISIBLE);
            if(size > 1){
            	mClew.setVisibility(View.VISIBLE);
            }else{
            	mClew.setVisibility(View.GONE);
            }
        }else{
            mNone.setVisibility(View.VISIBLE);
            mViewPager.setVisibility(View.GONE);
            mClew.setVisibility(View.GONE);
        }
    }

    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
        	((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            GuideInfoView v = (GuideInfoView)mListViews.get(arg1);
            v.initData(mPusherList.get(arg1));
            return v;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    private final class DataQueryHandler extends AsyncQueryHandler {
        public DataQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case PLIST_QUERY_TOKEN:
                    updateList(cursor);
                    break;
                default:
                    Log.e(TAG, "onQueryComplete called with unknown token " + token);
            }
        }

        @Override
        protected void onDeleteComplete(int token, Object cookie, int result) {
        }
    }

    public void onDestroy(){
        super.onDestroy();
        mViewPager = null;
        mDataQueryHandler = null;
        mPusherList.clear();
        mPusherList = null;
        mPusherViewList.clear();
        mPusherViewList = null;
    }
}
